package webservice;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.google.android.maps.MapView.ReticleDrawMode;

import android.app.Application;
import android.util.Log;
import database.DatabaseHelpers.VIPmeSQLiteHelper;
import database.Models.User;

public class RESTfulClient{

	int TIMEOUT_MILLISEC = 10000; // = 10 seconds

	public User serverVerifyUser(String username, String password) throws IOException {
		
		User user = User.getInstance();
	
		try {
			JSONObject json = new JSONObject();
			json.put("Username", username);
			json.put("Password",  password);
			HttpParams httpParams = new BasicHttpParams();
			HttpConnectionParams.setConnectionTimeout(httpParams,
					TIMEOUT_MILLISEC);
			HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
			HttpClient client = new DefaultHttpClient(httpParams);
			//
			//String url = "http://10.0.2.2:8080/sample1/webservice2.php?json={\"UserName\":1,\"FullName\":2}";
			String url = "http://vipme.bplaced.net/webservices/verifyuser.php";

			HttpPost httppost = new HttpPost(url);
			httppost.setEntity(new ByteArrayEntity(json.toString().getBytes(
					"UTF8")));
			httppost.setHeader("json", json.toString());
			//HttpResponse response = client.execute(httppost);

		
			
				Log.i(getClass().getSimpleName(), "send  task - start");
	
				ResponseHandler<String> responseHandler = new BasicResponseHandler();
				String responseBody = client.execute(httppost,
						responseHandler);
				// Parse
				
				JSONObject jsonresp = new JSONObject(responseBody);
				JSONArray jArray = jsonresp.getJSONArray("retusers");
				ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();

				for (int i = 0; i < jArray.length(); i++) {
					HashMap<String, String> map = new HashMap<String, String>();
					JSONObject e = jArray.getJSONObject(i);
					String s = e.getString("account");
					JSONObject jObject = new JSONObject(s);

					
					map.put("UserName", jObject.getString("Username"));
					map.put("Password", jObject.getString("Password"));
					map.put("mail", jObject.getString("Email"));
					map.put("age", jObject.getString("Alter"));
					map.put("home", jObject.getString("Wohnort"));
					map.put("sex", jObject.getString("Geschlecht"));

					mylist.add(map);
				}
				System.out.println("verify user"+ responseBody);
				if(!mylist.isEmpty()){
				user.setUserName(mylist.get(0).get("UserName"));
				user.setPassword(mylist.get(0).get("Password"));
				user.setPrimaryMail(mylist.get(0).get("mail"));
				user.setAge(mylist.get(0).get("age"));
				user.setHome(mylist.get(0).get("home"));
				user.setSex(mylist.get(0).get("sex"));
				}
				
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
			// Log.i(getClass().getSimpleName(), "send  task - end");
			catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		 
		return user;

	}
	
	public ArrayList<HashMap<String, Object>> serverGetLocationsAndVisits(String username, String password) throws IOException {
		User user = User.getInstance();
		ArrayList<HashMap<String, Object>> mylist = null;
		try {
			JSONObject json = new JSONObject();
			json.put("Username", username);
			json.put("Password",  password);
			HttpParams httpParams = new BasicHttpParams();
			HttpConnectionParams.setConnectionTimeout(httpParams,
					TIMEOUT_MILLISEC);
			HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
			HttpClient client = new DefaultHttpClient(httpParams);
			//
			//String url = "http://10.0.2.2:8080/sample1/webservice2.php?json={\"UserName\":1,\"FullName\":2}";
			String url = "http://vipme.bplaced.net/webservices/getinformation_test.php";

			HttpPost httppost = new HttpPost(url);
			httppost.setEntity(new ByteArrayEntity(json.toString().getBytes(
					"UTF8")));
			httppost.setHeader("json", json.toString());
			//HttpResponse response = client.execute(httppost);

		
			
				Log.i(getClass().getSimpleName(), "send  task - start");
	
				ResponseHandler<String> responseHandler = new BasicResponseHandler();
				String responseBody = client.execute(httppost,
						responseHandler);
				// Parse
				System.out.println("get information"+ responseBody);
				JSONObject jsonresp = new JSONObject(responseBody);
				JSONArray jArray = jsonresp.getJSONArray("retinfo");
			 mylist = new ArrayList<HashMap<String, Object>>();

				for (int i = 0; i < jArray.length(); i++) {
					HashMap<String, Object> map = new HashMap<String, Object>();
					JSONObject e = jArray.getJSONObject(i);
					//String s = e.get;
					//JSONObject jObject = new JSONObject(s);

					
					map.put("disco", e.getString("disko"));
					//map.put("punktestand", e.getString("punktestand"));
					map.put("status", e.getString("status"));
					map.put("gebucht_am", e.get("gebucht_am"));
					map.put("punkte", e.get("punkte"));
//					/**
					/**
					 * jetzt besuche in json array
					 * 
					 */
	//				JSONArray checkins = jsonresp.getJSONArray("gebucht_am");
					
//					for (int c = 0; c < checkins.length(); c++) {
//						JSONObject jobj = jArray.getJSONObject(c);
//						map.put("disco", jobj.names());
//					}
//					map.put("punktestand", jObject.getString("punktestand"));
//					map.put("punktestand", jObject.getString("punktestand"));
					mylist.add(map);
				}
				
//				if(!mylist.isEmpty()){
//				user.setUserName(mylist.get(0).get("UserName"));
//				user.setPassword(mylist.get(0).get("Password"));
//				}
				System.out.println(mylist.toString());
				
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
			// Log.i(getClass().getSimpleName(), "send  task - end");
			catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		 
		return mylist;

	}
	public ArrayList<HashMap<String, Object>> serverGetLocationAndActions() throws IOException {
		User user = User.getInstance();
		ArrayList<HashMap<String, Object>> mylist = null;
		try {
			JSONObject json = new JSONObject();
//			json.put("Username", username);
//			json.put("Password",  password);
			HttpParams httpParams = new BasicHttpParams();
			HttpConnectionParams.setConnectionTimeout(httpParams,
					TIMEOUT_MILLISEC);
			HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
			HttpClient client = new DefaultHttpClient(httpParams);
			//
			//String url = "http://10.0.2.2:8080/sample1/webservice2.php?json={\"UserName\":1,\"FullName\":2}";
			String url = "http://vipme.bplaced.net/webservices/getActions.php";

			HttpPost httppost = new HttpPost(url);
			httppost.setEntity(new ByteArrayEntity(json.toString().getBytes(
					"UTF8")));
			httppost.setHeader("json", json.toString());
			//HttpResponse response = client.execute(httppost);

		
			
				Log.i(getClass().getSimpleName(), "send  task - start");
	
				ResponseHandler<String> responseHandler = new BasicResponseHandler();
				String responseBody = client.execute(httppost,
						responseHandler);
				// Parse
				System.out.println("get actions"+ responseBody);
				JSONObject jsonresp = new JSONObject(responseBody);
				System.out.println(responseBody);
				JSONArray jArray = jsonresp.getJSONArray("retinfo");
			 mylist = new ArrayList<HashMap<String, Object>>();

				for (int i = 0; i < jArray.length(); i++) {
					HashMap<String, Object> map = new HashMap<String, Object>();
					JSONObject e = jArray.getJSONObject(i);
					//String s = e.get;
					//JSONObject jObject = new JSONObject(s);

					
					map.put("name", e.getString("name"));
					//map.put("punktestand", e.getString("punktestand"));
					map.put("text", e.getString("text"));
					map.put("punkte", e.get("punkte"));
					map.put("location", e.get("location"));
//					/**
					/**
					 * jetzt besuche in json array
					 * 
					 */
	//				JSONArray checkins = jsonresp.getJSONArray("gebucht_am");
					
//					for (int c = 0; c < checkins.length(); c++) {
//						JSONObject jobj = jArray.getJSONObject(c);
//						map.put("disco", jobj.names());
//					}
//					map.put("punktestand", jObject.getString("punktestand"));
//					map.put("punktestand", jObject.getString("punktestand"));
					mylist.add(map);
				}
				
//				if(!mylist.isEmpty()){
//				user.setUserName(mylist.get(0).get("UserName"));
//				user.setPassword(mylist.get(0).get("Password"));
//				}
				System.out.println(mylist.toString());
				
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
			// Log.i(getClass().getSimpleName(), "send  task - end");
			catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		 
		return mylist;

	}
	public boolean serverCreateCheckIn(String username, String password, String qr_code) throws IOException {
		
Boolean checkedIn = false;
	
		try {
			JSONObject json = new JSONObject();
			json.put("Username", username);
			json.put("Password",  password);
			json.put("qr_code_id",  qr_code);
			json.put("act_id",  1);
			HttpParams httpParams = new BasicHttpParams();
			HttpConnectionParams.setConnectionTimeout(httpParams,
					TIMEOUT_MILLISEC);
			HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
			HttpClient client = new DefaultHttpClient(httpParams);
			//
			//String url = "http://10.0.2.2:8080/sample1/webservice2.php?json={\"UserName\":1,\"FullName\":2}";
			String url = "http://vipme.bplaced.net/webservices/checkIn.php";

			HttpPost httppost = new HttpPost(url);
			httppost.setEntity(new ByteArrayEntity(json.toString().getBytes(
					"UTF8")));
			httppost.setHeader("json", json.toString());
			//HttpResponse response = client.execute(httppost);

		
			
				Log.i(getClass().getSimpleName(), "send  task - start");
	
				ResponseHandler<String> responseHandler = new BasicResponseHandler();
				String responseBody = client.execute(httppost,
						responseHandler);
				// Parse
				
			//	JSONObject jsonresp = new JSONObject(responseBody);
			
				
				System.out.println("checkin: "+ responseBody);
			if(responseBody.equalsIgnoreCase("\"success\""))
				checkedIn = true;
				
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
			// Log.i(getClass().getSimpleName(), "send  task - end");
			catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		 
		return checkedIn;

	}
public String serverCreateUser() throws IOException {
	String resp = "";
		User user = User.getInstance();
	
		try {
			JSONObject json = new JSONObject();
			json.put("Username", user.getUserName());
			json.put("Password",  user.getPassword());
			json.put("Mail",  user.getPrimaryMail());
			json.put("Alter", user.getAge());
			json.put("Geschlecht",  user.getSex());
			json.put("Wohnort",  user.getHome());
			HttpParams httpParams = new BasicHttpParams();
			HttpConnectionParams.setConnectionTimeout(httpParams,
					TIMEOUT_MILLISEC);
			HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
			HttpClient client = new DefaultHttpClient(httpParams);
			//
			//String url = "http://10.0.2.2:8080/sample1/webservice2.php?json={\"UserName\":1,\"FullName\":2}";
			String url = "http://vipme.bplaced.net/webservices/createUser.php";

			HttpPost httppost = new HttpPost(url);
			httppost.setEntity(new ByteArrayEntity(json.toString().getBytes(
					"UTF8")));
			httppost.setHeader("json", json.toString());
			//HttpResponse response = client.execute(httppost);

		
			
				Log.i(getClass().getSimpleName(), "send  task - start");
	
				ResponseHandler<String> responseHandler = new BasicResponseHandler();
				String responseBody = client.execute(httppost,
						responseHandler);
				// Parse
				
				 resp = responseBody;
			
				System.out.println("create user"+ resp);
				
				
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
			// Log.i(getClass().getSimpleName(), "send  task - end");
			catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		 
		return resp;

	}
	
}